<template>
  <div class="form-mask">
    <div class="form-wrapper">
      <div class="form-left">
        <!-- <img class="logo" :src="BASE_URL + configuration.logo" /> -->
        <div class="header" v-html="configuration.title"></div>
        <div class="design">Design by ColinWoo</div>
      </div>
      <div class="form-right">
        <el-menu
          :default-active="activeIndex1"
          class="el-menu-demo"
          mode="horizontal"
          @select="handleSelect"
          background-color="#021c358f"
          text-color="#fff"
          active-text-color="#0e92b3">
            <el-menu-item index="1" active>账号密码</el-menu-item>
            <el-menu-item index="2">微信登录</el-menu-item>
        </el-menu>
        <div class="input-wrapper">
          <div class="border-wrapper">
            <input type="text" v-model="form.username" name="username" class="border-item" placeholder="username" />
          </div>
          <div class="border-wrapper">
            <input type="password" v-model="form.password" name="password" class="border-item" placeholder="password" />
          </div>
          <div class="border-wrapper">
            <input
              @keyup.enter="login"
              type="text"
              v-model="form.VerifyCode"
              name="VerifyCode"
              class="border-item code-input"
              placeholder="code"
            />
            <img @click="getKey" class="border-item code-img" :src="imgSrc.data" alt="" />
          </div>
        </div>
        <div class="action">
          <div class="btn" @click="login" >login</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reactive, getCurrentInstance, ref } from 'vue';
import { BASE_IMAGE_URL } from '@/utils/config';
let md5 = require('md5');
export default {
  name: 'Login',
  setup() {
    const { ctx } = getCurrentInstance();
    let activeIndex1 = ref(1);
    let form = reactive({
      username: '',
      password: '',
      VerifyCode: '',
      key: ''
    });
    let imgSrc  = reactive({
      data:''
    });
    async function getKey() {
      let key = await ctx.$fetch.get('/public/getCaptcha');
      form.VerifyCode = '';
      imgSrc.data = key.Data.data;
      form.key = key.Data.captchaId
    }
    getKey();

    let configuration = reactive({
      title: '',
      logo: ''
    });
    async function getConfig() {
      await ctx.$store.dispatch('GET_CONFIGURE');
      configuration.title = ctx.$store.state.configuration.title;
      configuration.logo = ctx.$store.state.configuration.login_logo;
    }
    getConfig();

    async function login() {
      let data = { ...form };
      data.password = md5(data.password);
      try {
        let user = await ctx.$fetch.get('/public/login', data)
        ctx.$message.success(user.Msg);
        ctx.$store.dispatch('SET_TOKEN', form.key);
        ctx.$store.dispatch('SET_USERROLEMENUS', user.Data);
        await ctx.$store.dispatch('GET_USERINFO');
        ctx.$router.push({
          name: 'Home'
        });
      } catch (error) {
        ctx.$message.error(error.Msg);
        getKey();
      }
    }

    return {
      BASE_IMAGE_URL,
      imgSrc,
      form,
      login,
      configuration,
      getConfig,
      getKey,
      activeIndex1
    };
  }
};
</script>

<style lang="scss" scoped>
.form-mask {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background-color: rgb(41, 45, 62);
}
.form-wrapper {
  width: 800px;
  height: 500px;
  // padding: 30px;
  color: #fff;
  background-color: #001529;
  border-radius: 5px;
}
.form-wrapper .form-left{
  width: 450px;
  height: 500px;
  display: inline-block;
  vertical-align: top;
  padding: 30px;
  padding-top: 120px;
  color: #fff;
  background-color: #001529;
  border-radius: 2px;
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
}
.form-wrapper .form-left .design{
  font-size: 16px;
  font-weight: 100;
  padding-left: 60px;
}
.form-wrapper .form-right{
  display: inline-block;
  height: 500px;
  vertical-align: top;
  width: 350px;
  padding: 30px;
  color: #fff;
  background-color: #021c358f;
  border-radius: 2px;
  border-top-right-radius: 5px;
  border-bottom-right-radius: 5px;
}

.form-wrapper .header {
  font-size: 25px;
  line-height: 80px;
  text-align: left;
  // text-transform: uppercase;
}

.form-wrapper .logo {
  width: 75px;
  height: 90px;
}

.form-wrapper .input-wrapper input {
  width: 100%;
  font-size: 15px;
  color: #fff;
  text-align: center;
  background-color: rgb(41, 45, 62);
  border: 0;
  outline: none;
}

.form-wrapper .input-wrapper input::placeholder {
  text-transform: uppercase;
}

.form-wrapper .input-wrapper .border-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 50px;
  margin-bottom: 20px;
  background-color: #292d3e;
  // background-image: linear-gradient(to right,#92ffad , #0a643b);
  // border-radius: 30px;
  .code-input {
    flex: 1;
    // border-radius: 30px 0 0 30px !important;
  }
  .code-img {
    width: 100px !important;
    cursor: pointer;
    // border-radius: 0 30px 30px 0 !important;
  }
}

.form-wrapper .input-wrapper .border-wrapper .border-item {
  width: calc(100% - 4px);
  height: calc(100% - 4px);
  // border-radius: 30px;
}

.form-wrapper .action {
  display: flex;
  justify-content: center;
}

.form-wrapper .action .btn {
  width: 60%;
  line-height: 50px;
  text-align: center;
  text-transform: uppercase;
  cursor: pointer;
  border: 2px solid #0e92b3;
  border-radius: 30px;
}

.form-wrapper .action .btn:hover {
  background-color: #0e92b3;
}

.form-wrapper .icon-wrapper {
  width: 60%;
  padding: 20px;
  margin: 0 auto;
  margin-top: 20px;
  text-align: center;
  border-top: 1px dashed rgb(146, 146, 146);
}

.form-wrapper .icon-wrapper i {
  padding: 5px;
  font-size: 20px;
  color: rgb(187, 187, 187);
  cursor: pointer;
  border: 1px solid #fff;
  border-radius: 20px;
}
.el-menu-demo{
  margin-bottom: 40px;
  border-bottom: 0px;
}
.el-menu-demo .el-menu-item{
  width:50%
}
.el-menu-demo .el-menu-item:hover{
  background-color: #001529 !important;
}
.code-box {
  display: flex;
}
</style>
